import type {
  FrontProcessing,
  GetFaceIdByCellIndexResult,
} from "@/FrontProcessing";
import { ArrayPlus, devtool, OutLineActor, VTActor } from "cax-base";
import { GyActor } from "@/FrontProcessing/Actors/GyActor";
import { State } from "@/FrontProcessing/State/State";

export class SelectorComp {
  engine: FrontProcessing;
  constructor(engine: FrontProcessing) {
    this.engine = engine;
  }

  // matchCellActors() {}

  // getAllRelativeTriangleIds() {}

  /**
   * # 获取当前选中的cell索引 ---- 单选
   * @param actorList - 搜索的actor列表
   * @param pickedCellId - 当前选中的cell索引
   * @param state - 状态类
   * @param gyActors - 目标列表
   */
  getFaceIdByCellIndex(
    actorList: ArrayPlus<VTActor>,
    pickedCellId: number,
    state: State,
    gyActors: GyActor[]
  ): GetFaceIdByCellIndexResult {
    let back: any = {};
    if (!actorList.value.length || !gyActors.length) return back;
    for (let i = 0; i < actorList.value.length; i++) {
      if (actorList.value[i] == gyActors[0] && gyActors[0] instanceof GyActor) {
        const back2 = gyActors[0].findFaceByTriangleIndex(pickedCellId);
        if (back2) {
          back = { ...back2 };
          back.actorIndex = i;
          back.partId = gyActors[0].partId;
          back.partEntityId = gyActors[0].partEntityId;
          state.currentSelectedActors.push(actorList.value[i]);
          break;
        }
      }
    }
    return back;
  }

  getPartIdByCellIndex(deletedPart: any, gyActors: GyActor): any {
    let indices: number[] = [];
    // 选中实体要删除已经删除过的面的id
    indices =
      deletedPart?.faceIds
        .map((value: any) => gyActors?.faceIds.indexOf(value))
        .filter((index: any) => index !== -1) || [];
    console.log("indices", indices);
    let a: any = [];
    const faceTrianglesMap = gyActors.faceTrianglesMap;
    console.log("faceTrianglesMap", faceTrianglesMap);
    for (let i = 0; i < faceTrianglesMap.size; i++) {
      if (indices.length === 0) {
        a = a.concat(faceTrianglesMap.get(i));
      } else {
        if (!indices.includes(i)) {
          a = a.concat(faceTrianglesMap.get(i));
        }
      }
    }
    return {
      partId: gyActors.partId,
      partEntityId: gyActors.partEntityId,
      triangleIds: a,
    };
  }

  /**
   * 通过面id查找对应的三角形id
   * @param actorList - 搜索的actor列表
   * @param faceId - 面id
   */
  getTriangleIdsByFaceId(
    actorList: ArrayPlus<GyActor>,
    partId: number,
    faceId: number
  ) {
    let back: any = {};
    if (!actorList.value.length) return back;
    for (let i = 0; i < actorList.value.length; i++) {
      if (actorList.value[i].partId == partId) {
        const back2 = actorList.value[i].findTriangleByFaceId(faceId);
        console.log("back2", back2);
        if (back2) {
          back = { ...back2 };
          back.actorIndex = i;
          back.partEntityId = actorList.value[i].partEntityId;
          back.partId = actorList.value[i].partId;
          break;
        }
      }
    }
    return back;
  }

  /**
   * @description 根据索引返回轮廓线id
   * @param index {number}
   * @param outLinesAc {OutLineActor}
   * @returns number | null
   */
  getEdgeIdByEdgeIndex(index: number, outLinesAc: OutLineActor): number | null {
    const originData = outLinesAc.edgeData;
    if (!originData.edgeIds) return null;
    return originData.edgeIds[index];
  }
}
